/*
* Copyright (C) 2020 ~ 2023 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include "MkGb28181/MkGbEndUser.h"
#include "MkUtil/MkLog.h"
#include "MkEvent/MkEventPool.h"
#include "MkEvent/MkTimer.h"
#include "MkGbMedia/MkGbMediaEndUser.h"
#include "MkMedia/MkFileSource.h"

CMkTimer* g_pTimer = nullptr;
int main(int argc, char** argv)
{
    if (argc < 3)
    {
        printf("Usage:%s 264FileName ServerIp ServerPort Count\n", argv[0]);
        return 0;
    }
    MkString FileName = argv[1];
    MkString Ip = argv[2];
    Uint16 Port = std::atoi(argv[3]);
    int nCount = std::stoi(argv[4]);
#ifdef _WIN32
    WSADATA WSAData;
    WSAStartup(0x101, &WSAData);
    //MkString FileName = "C:/Workspace/media/aa.264";
#else
    //MkString FileName = "/root/wzg/mksource/bin/media/aa.264";
#endif

    //MkString Ip = "192.168.100.120";
    ////Ip = "192.168.0.24";
    //Uint16 Port = 5080;
    //int nCount = 1;
    MkLogChangeLevel(LOG_DEBUG);
    MkLogChangeWriteDst(TO_SCREEN);
    CMkEventPool* pEventPool = new CMkEventPool;
    pEventPool->Start();
    CMkTimer *pTimer = new CMkTimer;
    g_pTimer = pTimer;
    pTimer->StartTimerThread();

    CMkMediaSource::SetCachePacket(FALSE);

    CMkGbMediaEndUser *pGbEndUser = new CMkGbMediaEndUser(pEventPool, pTimer);
    MkGbStartParam Param;
    Param.LocalHost = "";
    Param.LocalPort = 55560;
    Param.SipId = "34020000004000000001";
    Param.SipRealm = "3402000000";
    Param.MappingHost = "";
    Param.MappingPort = 0;
    Param.RegisterExpires = 3600;
    Param.SubscribeExpires = 3600;
    pGbEndUser->Start(Param);
    int Index = 0;
    while (Index < nCount) {
        Int8 pNumber[Len16] = { 0 };
        snprintf(pNumber, Len16, "%07d", Index++);
        MkString SipUri = "sip:3402000000132" + MkString(pNumber) + "@" + Ip + ":" + std::to_string(Port);
        FILE *fp = fopen(FileName.c_str(), "rb");
        if (nullptr == fp) {
            break;
        }
        CMkH264FileSource *pMediaSource = new CMkH264FileSource(fp, pTimer);
        pMediaSource->ScanFile();
        pGbEndUser->StartPublish(SipUri, FALSE, pMediaSource, [pMediaSource](Uint32 ErrorCode, const MkString& SessionId) {
            MkDebugLog("start publish error code:%d session:%s\n", ErrorCode, SessionId.c_str());
            pMediaSource->StartMediaSource(nullptr);
        }, [pMediaSource](const MkString& SessionId) {
            MkDebugLog("Session:%s has stoped\n", SessionId.c_str());
            pMediaSource->StopMediaSource();
        });
        CMkThread::ThreadSleep(1000);
    }
    while (1) {
        CMkThread::ThreadSleep(10000);
    }
    pTimer->StopTimerThread();
    pEventPool->Stop();
    MkDelete(pEventPool);
    MkDelete(pTimer);
#ifdef _WIN32
    WSACleanup();
#endif
    return 0;
}